using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace TVProgram.Base
{
    class ProgramItem
    {
        protected DateTime _startTime;
        protected DateTime _endTime;
        protected string _content;
        public DateTime StartTime
        {
            get { return this._startTime; }
            set { this._startTime = value; }
        }
        //endTime
        public DateTime EndTime
        {
            get { return this._endTime; }   // Tra ve Endtime
            set { this._endTime = value; }  // Nhap Endtime
        }
        //Content
        public string Content
        {
            get { return this._content; }
            set { this._content = value; }
        }

        public DateTime GetDateTimeFromString(string strTime)
        {
            string strHour = strTime.Substring(0, 2);
            int m = strTime.Length;
            string strMinute = strTime.Substring(m - 2, 2);
            string FullTime = strHour + ":" + strMinute;
            DateTime Time = Convert.ToDateTime(FullTime);
            return Time;

        }
        public string getInfo()
        {
            string strHour = this.StartTime.Hour.ToString();
            if (strHour.Length == 1)
                strHour = "0" + strHour;
            else
                strHour = "" + strHour;

            string strMinute = this.StartTime.Minute.ToString();
            if (strMinute.Length == 1)
                strMinute = "0" + strMinute;
            else
                strMinute = "" + strMinute;

            string ret = "";
            ret += strHour;
            ret += ":";
            ret += strMinute;
            ret += " " + this._content;
            return ret;
        }
    }
    class BaseChannel
    {
        protected string _name = "";
        protected int _id = 0;
        protected string _url = "";
        protected DateTime _date;
        protected string _RequestInfo = "";
        protected List<ProgramItem> _items = new List<ProgramItem>();

        protected void clearList()
        {
            if (this._items.Count > 0)
                this._items.Clear();
        }
        protected string GetHtmlFromUrl()
        {
            //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this._url);
            //HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //string html = reader.ReadToEnd();
            //reader.Close();
            //response.Close();
            //return html;

            //Get channel ID and date

            //Set up a request object
            WebRequest request = WebRequest.Create(this._url);

            //Specify the method: POST or GET
            request.Method = "POST";

            //data we want to send is included here
            string data = this._RequestInfo;
            
            //convert the data into byte array
            byte[] byteArray = Encoding.UTF8.GetBytes(data);

            //set Content-Length and Content-Type
            request.ContentLength = byteArray.Length;
            request.ContentType = "application/x-www-form-urlencoded";
             
            //get the request stream and write data to it and close it
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
             
            //Now the real request is called here when we call the GetResponse method
            WebResponse response = request.GetResponse();
            //Get the data from the Response


            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);

            string html = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return html;








            dataStream = response.GetResponseStream();
            //Open stream using XMLTextReader
            //XmlTextReader reader1 = new XmlTextReader(dataStream);
            //XmlDocument doc = new XmlDocument();
            //doc.Load(reader1);

           // string html = doc.Save;

            //reader.Close();
            //response.Close();
            return html;
        }
        public virtual void GetTextFromHtml(string html)
        {

        }
        public bool Load()
        {
            this.clearList();
            string html = this.GetHtmlFromUrl();
            this.GetTextFromHtml(html);
            return true;
        }
        public bool writeToFile()
        {
            //open file
           string strYear = Convert.ToString(this._date.Year);
           if (strYear.Length == 1)
               strYear = "0" + strYear;
           else
               strYear = "" + strYear;

            string strMonth = Convert.ToString(this._date.Month);
            if (strMonth.Length == 1)
                strMonth = "0" + strMonth;
            else
                strMonth = "" + strMonth;

            string strDay = Convert.ToString(this._date.Day);
            if (strDay.Length == 1)
                strDay = "0" + strDay;
            else
                strDay = "" + strDay;

            string directoryPath = "D:\\TV Program\\" + strYear + strMonth + strDay;
            string DayInFilename = strDay + strMonth + strYear;

            if (!System.IO.Directory.Exists(directoryPath))
                System.IO.Directory.CreateDirectory(directoryPath);
            string filePath = directoryPath + @"\" + this._name + " " + DayInFilename + @".txt";
            StreamWriter sw = new StreamWriter(filePath);

            // write to file
            string Header1 = "Channel" + " " + this._name;
            string Date = Convert.ToString(this._date.Day) + "/" + Convert.ToString(this._date.Month) + "/" + Convert.ToString(this._date.Year);
            string Header2 = "Date" + " " + Date;
            sw.WriteLine(Header1);
            sw.WriteLine(Header2);

            //for list
            //item[i] => info
            //write info => file
            int i;
            int j = this._items.Count;
            for (i = 0; i < j; i++)
            {
                ProgramItem obj = this._items[i] as ProgramItem;
                string strItem = obj.getInfo();
                sw.WriteLine(strItem);
            }
            
            // close file
            sw.Flush();
            sw.Close();
            return true;
        }
    }
}

